/*
 * PlayerController.java
 *
 *  Copyright (c) 2008 Corey Oliver and Anshul Kumaria
 *
 *  This file is part of Balls of Fury.
 *
 *  Balls of Fury is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Balls of Fury is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 *
 */

/*
 * EDIT:  03/13/2008 - This is an example of how an edit performed to the class 
 * would appear.
 */
package ballsoffury.game;

import com.jme.scene.Node;
import com.jme.math.Vector2f;
import com.jme.input.MouseInput;
import com.jme.system.DisplaySystem;

/**
 * <code>PlayerController</code> controls the model movement via the mouse.
 *
 * @author Corey Oliver
 * @version $Revision:$ $Date:$
 */
public class PlayerController extends BallsOfFuryGameController {

    private static final long serialVersionUID = 1L;
    private Node playerModel;
    private Vector2f mousePosition;
    private Vector2f centerPosition;
    private float xSensitivity;
    private float ySensitivity;

    public PlayerController(BallsOfFuryGameState ballsOfFuryGameState, 
            Node playerNode, float xSensitivity, float ySensitivity) {
        super(ballsOfFuryGameState);
        this.playerModel = playerNode;
        this.xSensitivity = xSensitivity; // The lower the value the less sensitive
        this.ySensitivity = ySensitivity; // The lower the value the less sensitive
        mousePosition = new Vector2f(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
        centerPosition = new Vector2f(DisplaySystem.getDisplaySystem().getWidth() / 2, 
                DisplaySystem.getDisplaySystem().getHeight() / 2);
    }
    
    public PlayerController(BallsOfFuryGameState ballsOfFuryGameState, 
            Node playerNode) {
        this(ballsOfFuryGameState, playerNode, 1f, 1f);
    }

    @Override
    public void update(float time) {
        mousePosition.set(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
        float xDirection =  xSensitivity * (mousePosition.x - centerPosition.x);
        float yDirection = ySensitivity * (mousePosition.y - centerPosition.y);
        
        playerModel.setLocalTranslation(xDirection, playerModel.getLocalTranslation().y, -yDirection);
        ballsOfFuryGameState.getRootNode().updateRenderState();
    }
}
